from main import db
from sqlalchemy import Column, String, Integer, Boolean, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from flask_sqlalchemy import SQLAlchemy
import json
from datetime import datetime
from main.utils import TimeHelper
from flask import Flask
import config


class User(db.Model):
    __tablename__ = "user"
    id = Column(Integer(), nullable=False, primary_key=True, autoincrement=True)
    username = Column(String(64))
    password = Column(String(64))
    nick = Column(String(20))
    avatar = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict

class Friend(db.Model):
    __tablename__ = "friend"
    id = Column(Integer(), primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('user.id'))
    friend_id = Column(Integer, ForeignKey('user.id'))
    time = Column(db.DateTime, default=datetime.now())
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict

class Medicine(db.Model):
    __tablename__ = "medicine"
    id = Column(Integer(), primary_key=True, autoincrement=True)
    medicine_name = Column(String(64), index=True,)
    warning = Column(String(255))
    time = Column(db.DateTime, default=datetime.now())
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class FriendNotice(db.Model):
    __tablename__ = "friendnotice"
    id = Column(Integer(), primary_key=True, autoincrement=True)
    user_id = Column(Integer(), ForeignKey('user.id'))
    is_notice = Column(Boolean(),default= False)
    time = Column(db.DateTime, default=datetime.now())
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class Notice(db.Model):
    __tablename__ = "notice"
    id = Column(Integer(), primary_key=True, autoincrement=True)
    title = Column(String(255))
    hour = Column(Integer())
    minute = Column(Integer())
    user_id = Column(Integer(), ForeignKey('user.id'))
    is_notice = Column(Boolean(), default=False)
    time = Column(db.DateTime, default=datetime.now())

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict

if __name__ == "__main__":
    db.create_all()
